﻿using liaowa2.Entity.DataModel;
using liaowa2.Entity.DTO;
using liaowa2.Entity.DTO.ManagerRole;
using liaowa2.Entity.DTO.ManagerRoleNav;
using liaowa2.Interface.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace liaowa2.Web.Controllers
{
    public class ManagerRoleController : BaseController
    {
        IManagerRoleInterface managerRoleAppService;
        IManagerRoleValueInterface managerRoleValueAppService;
        IManagerUserInterface managerUserAppService;

        public ManagerRoleController(IManagerRoleInterface managerRoleAppService, 
            IManagerRoleValueInterface managerRoleValueAppService, IManagerUserInterface managerUserAppService)
        {
            this.managerRoleAppService = managerRoleAppService;
            this.managerRoleValueAppService = managerRoleValueAppService;
            this.managerUserAppService = managerUserAppService;
        }
        // GET: ManagerRole
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List()
        {
            var name = Request.Form["M_Name"];
            ViewBag.P_Name = name;
            return View();
        }

        public JsonResult ManagerRoleList(string name, int pageIndex, int pageSize)
        {
            var result = managerRoleAppService.GetAll(new GetAllInput()
            {
                RoleName = name,
                PageSize = pageSize,
                CurrentIndex = pageIndex
            });
            var record = (result.Count + pageSize - 1) / pageSize; //总也数
            result.Count = record;
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit(int? id)
        {
            Entity.DataModel.ManagerRole model = new Entity.DataModel.ManagerRole();
            if (id.HasValue)
            {
                model = managerRoleAppService.GetModel(id ?? 0);
                if (model != null)
                {
                    return View(model);
                }
                else
                {
                    AlertMessage("获取信息失败");
                    return null;
                }
            }
            return View(model);
        }

        public ActionResult Update(FormCollection collection)
        {
            ManagerRole model = new ManagerRole();
            int id = Convert.ToInt16(collection["id"]);
            int roleType = Convert.ToInt16(collection["ddlRoleType"]);
            string name = collection["txtRoleName"];
            string action_type = collection["rptList_cblActionType"];
            if (id > 0)
            {
                model = managerRoleAppService.GetModel(Convert.ToInt32(id));
                if (model != null)
                {
                    model.role_type = roleType;
                    model.role_name = name;
                    model.is_sys = 0;
                    string[] action = action_type.Split(',');
                    List<ManagerRoleValue> valList = new List<ManagerRoleValue>();
                    for (int i = 0; i < action.Length; i++)
                    {
                        valList.Add(new ManagerRoleValue { nav_name = action[i].Split('$')[1], action_type = action[i].Split('$')[0] });
                    }
                    if (managerRoleAppService.Update(model, valList))
                    {
                        AlertMessage("编辑成功");
                        return null;
                    }
                    else
                    {
                        AlertMessage("编辑失败");
                        return null;
                    };
                }
                else
                {
                    AlertMessage("获取信息失败");
                    return null;
                }
            }
            else
            {
                model.role_type = roleType;
                model.role_name = name;
                model.is_sys = 0;
                string[] action = action_type.Split(',');
                List<ManagerRoleValue> valList = new List<ManagerRoleValue>();
                for (int i = 0; i < action.Length; i++)
                {
                    valList.Add(new ManagerRoleValue { nav_name = action[i].Split('$')[1], action_type = action[i].Split('$')[0] });
                }
                if (managerRoleAppService.Add(model, valList))
                {
                    AlertMessage("添加成功");
                    return null;
                }
                else
                {
                    AlertMessage("添加失败");
                    return null;
                }
            }
        }

        public JsonResult Del(string id)
        {
            if (id.Length > 0)
            {
                id = id.Remove(id.Length - 1, 1);
                List<ManagerUser> list = managerUserAppService.GetList(" role_id in(" + id + ")").ToList();
                if (list.Count > 0)
                {
                    return Json(new { state = 0, msg = "请先删除和角色相关联的用户" }, JsonRequestBehavior.AllowGet);
                }
                if (managerRoleAppService.DeleteList(id))
                {
                    return Json(new { state = 1, msg = "" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { state = 0, msg = "删除失败" }, JsonRequestBehavior.AllowGet);
                }

            }
            return Json(new { state = 0, msg = "参数错误" }, JsonRequestBehavior.AllowGet);
        }
    }
}